package com.zhang.zhangapp.view.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.T;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

@ContentView(R.layout.activity_add_bank)
public class AddBankCardActivity extends BaseAppCompatActivityActivity {

    private final static int REQUEST_BANK_CARD_TYPE = 1000;
    @ViewInject(R.id.card_owner_name)
    EditText cardOwnerNameText;
    @ViewInject(R.id.card_no)
    EditText cardNoText;
    @ViewInject(R.id.bank_name)
    TextView bankNameText;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    String bankType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        titleText.setText("添加银行卡");
    }

    @Event(value = {R.id.action_bar_left_image, R.id.bank_name_layout,R.id.add_bank_sure})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.action_bar_left_image:
                this.finish();
                break;
            case R.id.bank_name_layout:
                selectBank();
                break;
            case R.id.add_bank_sure:
                addBank();
                break;
        }
    }

    private void selectBank(){
        Intent intent = new Intent(this,SelectBankTypeActivity.class);
        startActivityForResult(intent,REQUEST_BANK_CARD_TYPE);
    }

    private void addBank() {
        if (cardOwnerNameText.toString().equals("")) {
            T.showShort(this, "请填写持卡人");
            return;
        }
        if (cardNoText.toString().equals("")) {
            T.showShort(this, "请填写卡号");
            return;
        }
        if (bankNameText.getText().toString().equals("")) {
            T.showShort(this, "请选择银行");
            return;
        }

        if (showLoadingDialog()) {
            Map<String, String> params = new HashMap<>();
            params.put("memberId", MApplication.getInstance().getUser().getMemberId());
            params.put("name", cardOwnerNameText.getText().toString());
            params.put("bankno", cardNoText.getText().toString());
            params.put("bankType", bankType);
            CommonHttp.commonHttp("hzg/member/addBank", params, new MCallback() {
                @Override
                public void onSuccess(String data) {
                    T.showShort(AddBankCardActivity.this, "添加成功");
                    AddBankCardActivity.this.finish();
                }

                @Override
                public void onSuccess(String message, String code) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    dismisionDalog();
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && resultCode == RESULT_OK && requestCode == REQUEST_BANK_CARD_TYPE) {
            bankType = data.getStringExtra("type");
            bankNameText.setText(data.getStringExtra("name"));
        }
    }
}
